package com.fzy.core.tags;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang.StringUtils;

import com.fzy.core.entity.Page;
import com.fzy.core.utils.BooleanUtils;
import com.opensymphony.xwork.ActionContext;

public class ExtPageTag extends BodyTagSupport {

	private static final long serialVersionUID = -949653217908713132L;
	
	String page;
	String showpager;

	public int doStartTag() throws JspException {
		return super.doStartTag();
	}

	public void doInitBody() throws JspException {
		super.doInitBody();
	}

	public int doAfterBody() throws JspException {
		return super.doAfterBody();
	}

	public int doEndTag() throws JspException {
		Page p = (Page)ActionContext.getContext().getValueStack().findValue(this.page);
		
		if(p == null){
			p = new Page();
			p.setPageNo("1");
			p.setPageSize(Page.DEFAULT_PAGE_SIZE + "");
			p.setTotalCount(0);
		}
		
		String html = "";
		long count = p.getTotalCount();
		if(count < 0) count = 0;
		
		html += "<input type=\"hidden\" name=\""+page+".orderBy\" class=\"orderBy\" value=\""+StringUtils.trimToEmpty(p.getOrderBy())+"\" />";
		html += "<input type=\"hidden\" name=\""+page+".order\" class=\"order\" value=\""+StringUtils.trimToEmpty(p.getOrder())+"\" />";
		boolean booleanShowPager = BooleanUtils.toBoolean(showpager, true);
		if(booleanShowPager){
			html += "	<!-- pager -->";
			html += "	<div";
			html += "		class=\"x-toolbar x-docked x-toolbar-default x-docked-bottom x-toolbar-docked-bottom x-toolbar-default-docked-bottom x-box-layout-ct\"";
			html += "		style=\"position: relative !important; width: 100%; border: 0px;\">";
			html += "		<div class=\"x-box-inner \"";
			html += "			style=\"position: relative !important; width: 100%; height: 22px;\">";
			html += "			<table style=\"width: 100%\" cellspace=\"0\" cellpadding=\"0\"";
			html += "				border=\"0\">";
			html += "				<tr>";
			html += "					<td align=\"left\">";
			html += "						<table cellspace=\"0\" cellpadding=\"0\" border=\"0\">";
			html += "							<tr>";
			html += "								<td>";
			html += "									<div";
			html += "										class=\"x-toolbar-text x-box-item x-toolbar-item x-toolbar-text-default\"";
			html += "										style=\"position: relative !important; margin: 0pt; font-size: 12px;\">第</div>";
			html += "								</td>";
			html += "								<td>";
			html += "									<table cellpadding=\"0\"";
			html += "										style=\"width: 30px; margin: 0pt; position: relative !important;\"";
			html += "										class=\"x-field x-tbar-page-number x-form-item x-box-item x-toolbar-item x-field-default-toolbar x-hbox-form-item x-form-dirty\">";
			html += "										<tbody>";
			html += "											<tr>";
			html += "												<td colspan=\"3\" class=\"x-form-item-body \"";
			html += "													style=\"width: 100%;\"><table cellspacing=\"0\"";
			html += "														cellpadding=\"0\"";
			html += "														class=\"x-form-trigger-wrap x-unselectable\"";
			html += "														style=\"-moz-user-select: -moz-none; width: 100%; table-layout: fixed;\">";
			html += "														<tbody>";
			html += "															<tr>";
			html += "																<td class=\"x-form-trigger-input-cell\"";
			html += "																	style=\"width: 100%;\"><input type=\"text\"";
			html += "																	class=\"x-form-field x-form-text pageNo\"";
			html += "																	style=\"width: 100%;\"";
			html += "																	value=\""+p.getPageNo()+"\" name=\""+page+".pageNo\"";
			html += "																	>";
			html += "																</td>";
			html += "															</tr>";
			html += "														</tbody>";
			html += "													</table>";
			html += "												</td>";
			html += "											</tr>";
			html += "										</tbody>";
			html += "									</table>";
			html += "								</td>";
			html += "								<td>";
			html += "									<div";
			html += "										class=\"x-toolbar-text x-box-item x-toolbar-item x-toolbar-text-default\"";
			html += "										style=\"position: relative !important; margin: 0pt; font-size: 12px;\">页&nbsp;共&nbsp;"+p.getTotalPages()+"&nbsp;页,&nbsp;每页显示</div>";
			html += "								</td>";
			html += "								<td>";
			html += "									<table cellpadding=\"0\"";
			html += "										style=\"width: 30px; margin: 0pt; position: relative !important;\"";
			html += "										class=\"x-field x-tbar-page-number x-form-item x-box-item x-toolbar-item x-field-default-toolbar x-hbox-form-item x-form-dirty\">";
			html += "										<tbody>";
			html += "											<tr>";
			html += "												<td colspan=\"3\" class=\"x-form-item-body \"";
			html += "													style=\"width: 100%;\"><table cellspacing=\"0\"";
			html += "														cellpadding=\"0\"";
			html += "														class=\"x-form-trigger-wrap x-unselectable\"";
			html += "														style=\"-moz-user-select: -moz-none; width: 100%; table-layout: fixed;\">";
			html += "														<tbody>";
			html += "															<tr>";
			html += "																<td class=\"x-form-trigger-input-cell\"";
			html += "																	style=\"width: 100%;\"><input type=\"text\"";
			html += "																	class=\"x-form-field x-form-text pageSize\"";
			html += "																	style=\"width: 100%;\"";
			html += "																	value=\""+p.getPageSize()+"\" name=\""+page+".pageSize\"";
			html += "																	>";
			html += "																</td>";
			html += "															</tr>";
			html += "														</tbody>";
			html += "													</table>";
			html += "												</td>";
			html += "											</tr>";
			html += "										</tbody>";
			html += "									</table>";
			html += "								</td>";
			html += "								<td>";
			html += "									<div";
			html += "										class=\"x-toolbar-text x-box-item x-toolbar-item x-toolbar-text-default\"";
			html += "										style=\"position: relative !important; margin: 0pt; font-size: 12px;\">条记录&nbsp;共&nbsp;"+count+"&nbsp;条记录&nbsp;</div>";
			html += "								</td>";
			html += "							</tr>";
			html += "						</table></td>";
			html += "					<td align=\"right\">";
			html += "						<table cellspace=\"0\" cellpadding=\"0\" border=\"0\">";
			html += "							<tr>";
			html += "								<td>";
			html += getSmallButton((p.isHasPre() ? " onclick=\"javascript:jumpPage(1,this)\"" : ""), "首页", "page-first", p.isHasPre());
			html += "								</td>";
			html += "								<td>";
			html += getSmallButton((p.isHasPre() ? " onclick=\"javascript:jumpPage("+p.getPrePage()+",this)\"" : ""), "上一页", "page-prev", p.isHasPre());
			html += "								</td>";
			html += "								<td>";
			html += getSmallButton((p.isHasNext() ? " onclick=\"javascript:jumpPage("+p.getNextPage()+",this)\"" : ""), "下一页", "page-next", p.isHasNext());
			html += "								</td>";
			html += "								<td>";
			html += getSmallButton((p.isHasNext() ? " onclick=\"javascript:jumpPage("+p.getTotalPages()+",this)\"" : ""), "末页", "page-last", p.isHasNext());
			html += "								</td>";
			html += "								<td>";
			html += "									<div";
			html += "										style=\"border-width: 1px; position: relative !important; margin: 0pt;\"";
			html += "										class=\"x-toolbar-separator x-box-item x-toolbar-item x-toolbar-separator-horizontal\"></div>";
			html += "								</td>";
			html += "								<td>";
			html += getSmallButton(" onclick=\"javascript:jumpPage("+p.getPageNo()+",this)\"", "刷新", "loading", true);
			html += "								</td>";
			html += "							</tr>";
			html += "						</table></td>";
			html += "				</tr>";
			html += "			</table>";
			html += "		</div>";
			html += "	</div>";
		}
		try {
			pageContext.getOut().print(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getShowpager() {
		return showpager;
	}

	public void setShowpager(String showpager) {
		this.showpager = showpager;
	}
	
	private String getSmallButton(String clickHtml, String title, String icon, boolean isWorked){
		String userAgent = ((HttpServletRequest)pageContext.getRequest()).getHeader("User-Agent");
		boolean isIE = userAgent.indexOf("MSIE") > -1;
		String html = "";
		if(isIE){
			html += "<div";
			html += "	style=\"background-image: none; border-right-width: 1px; margin: 0px; border-top-width: 1px; border-bottom-width: 1px; border-left-width: 1px; position: relative !important;\"";
			html += "	class=\"x-btn x-box-item x-toolbar-item x-btn-default-toolbar-small x-icon x-btn-icon x-btn-default-toolbar-small-icon"+(isWorked ? "" : " x-item-disabled x-disabled x-btn-disabled x-btn-default-toolbar-small-disabled")+"\">";
			html += "	<table>";
			html += "		<tbody>";
			html += "			<tr>";
			html += "				<td style=\"padding-left: 3px; background-position: 0px -6px\"";
			html += "					class=\"x-frame-tl x-btn-tl x-btn-default-toolbar-small-tl x-btn-default-toolbar-small-icon-tl x-btn-default-toolbar-small-over-tl x-btn-default-toolbar-small-pressed-tl x-btn-default-toolbar-small-focus-tl\"></td>";
			html += "				<td style=\"background-position: 0px 0px; height: 3px\"";
			html += "					class=\"x-frame-tc x-btn-tc x-btn-default-toolbar-small-tc x-btn-default-toolbar-small-icon-tc x-btn-default-toolbar-small-over-tc x-btn-default-toolbar-small-pressed-tc x-btn-default-toolbar-small-focus-tc\"></td>";
			html += "				<td style=\"padding-left: 3px; background-position: right -9px\"";
			html += "					class=\"x-frame-tr x-btn-tr x-btn-default-toolbar-small-tr x-btn-default-toolbar-small-icon-tr x-btn-default-toolbar-small-over-tr x-btn-default-toolbar-small-pressed-tr x-btn-default-toolbar-small-focus-tr\"></td>";
			html += "			</tr>";
			html += "			<tr>";
			html += "				<td style=\"padding-left: 3px; background-position: 0px 0px\"";
			html += "					class=\"x-frame-ml x-btn-ml x-btn-default-toolbar-small-ml x-btn-default-toolbar-small-icon-ml x-btn-default-toolbar-small-over-ml x-btn-default-toolbar-small-pressed-ml x-btn-default-toolbar-small-focus-ml\"></td>";
			html += "				<td style=\"background-position: 0px 0px\"";
			html += "					class=\"x-frame-mc x-btn-mc x-btn-default-toolbar-small-mc x-btn-default-toolbar-small-icon-mc x-btn-default-toolbar-small-over-mc x-btn-default-toolbar-small-pressed-mc x-btn-default-toolbar-small-focus-mc\"><em><button";
			html += "							hidefocus=\"true\" style=\"height: 16px\" class=\"x-btn-center\"" + clickHtml;;
			html += "							title=\""+title+"\" type=\"button\">";
			html += "							<span class=\"x-btn-inner\">&nbsp;</span><span";
			html += "								class=\"x-btn-icon x-tbar-"+icon+"\"></span>";
			html += "						</button> </em></td>";
			html += "				<td style=\"padding-left: 3px; background-position: right 0px\"";
			html += "					class=\"x-frame-mr x-btn-mr x-btn-default-toolbar-small-mr x-btn-default-toolbar-small-icon-mr x-btn-default-toolbar-small-over-mr x-btn-default-toolbar-small-pressed-mr x-btn-default-toolbar-small-focus-mr\"></td>";
			html += "			</tr>";
			html += "			<tr>";
			html += "				<td style=\"padding-left: 3px; background-position: 0px -12px\"";
			html += "					class=\"x-frame-bl x-btn-bl x-btn-default-toolbar-small-bl x-btn-default-toolbar-small-icon-bl x-btn-default-toolbar-small-over-bl x-btn-default-toolbar-small-pressed-bl x-btn-default-toolbar-small-focus-bl\"></td>";
			html += "				<td style=\"background-position: 0px -3px; height: 3px\"";
			html += "					class=\"x-frame-bc x-btn-bc x-btn-default-toolbar-small-bc x-btn-default-toolbar-small-icon-bc x-btn-default-toolbar-small-over-bc x-btn-default-toolbar-small-pressed-bc x-btn-default-toolbar-small-focus-bc\"></td>";
			html += "				<td style=\"padding-left: 3px; background-position: right -15px\"";
			html += "					class=\"x-frame-br x-btn-br x-btn-default-toolbar-small-br x-btn-default-toolbar-small-icon-br x-btn-default-toolbar-small-over-br x-btn-default-toolbar-small-pressed-br x-btn-default-toolbar-small-focus-br\"></td>";
			html += "			</tr>";
			html += "		</tbody>";
			html += "	</table>";
			html += "</div>";
		}else {
			html += "									<div";
			html += "										style=\"border-width: 1px; position: relative !important; margin: 0pt;\"";
			html += "										class=\"x-btn x-box-item x-toolbar-item x-btn-default-toolbar-small x-icon x-btn-icon x-btn-default-toolbar-small-icon\">";
			html += "										<em><button" + clickHtml;
			html += "												hidefocus=\"true\" class=\"x-btn-center\" type=\"button\"";
			html += "												title=\""+title+"\" style=\"height: 16px;\">";
			html += "												<span style=\"\" class=\"x-btn-inner\">&nbsp;</span><span";
			html += "													class=\"x-btn-icon x-tbar-"+icon+"\"></span>";
			html += "											</button> </em>";
			html += "									</div>";
		}
		return html;
	}

}
